In [1]:
import numpy as np
from keras.layers import Dense, LeakyReLU, Input, Conv2DTranspose, Reshape, Conv2D
from keras.models import Model
In [2]:
latent_dim = 32
height = 32
width = 32
channels = 3
In [3]:
generator_input = Input(shape=(latent_dim,))
# First, transform the input into a 16x16 128-channels feature map
x = Dense(units = 128 * 16 * 16)(generator_input)
x = LeakyReLU()(x)
x = Reshape(target_shape = (16, 16, 128))(x)
# Then, add a convolution layer
x = Conv2D(filters = 256,
kernel_size = (5, 5),
padding = 'same')(x)
x = LeakyReLU()(x)
# Upsample to 32x32
x = Conv2DTranspose(filters = 256,
kernel_size = (4, 4),
strides = (2, 2),
padding = 'same')(x)
x = LeakyReLU()(x)
# Few more conv layers
x = Conv2D(filters = 256,
kernel_size = (5, 5),
padding = 'same')(x)
x = LeakyReLU()(x)
x = Conv2D(filters = 256,
kernel_size = (5, 5),
padding = 'same')(x)
x = LeakyReLU()(x)
# Produce a 32x32 1-channel feature map
x = Conv2D(filters = channels,
kernel_size = (7, 7),
activation = 'tanh',
padding = 'same')(x)
generator = Model(generator_input, x)
generator.summary()
In [4]:
from keras.layers import Flatten, Dropout
In [5]:
discriminator_input = Input(shape = (height, width, channels))
x = Conv2D(filters = 128,
kernel_size = (3, 3))(discriminator_input)
x = LeakyReLU()(x)
x = Conv2D(filters = 128,
kernel_size = (4, 4),
strides = (2, 2))(x)
x = LeakyReLU()(x)
x = Conv2D(filters = 128,
kernel_size = (4, 4),
strides = (2, 2))(x)
x = LeakyReLU()(x)
x = Conv2D(filters = 128,
kernel_size = (4, 4),
strides = (2, 2))(x)
x = LeakyReLU()(x)
x = Flatten()(x)
# One dropout layer - important trick!
x = Dropout(rate = 0.4)(x)
# Classification layer
x = Dense(units = 1,
activation = 'sigmoid')(x)
discriminator = Model(discriminator_input, x)
discriminator.summary()
In [6]:
from keras.optimizers import RMSprop
In [7]:
# To stabilize training, we use learning rate decay
# and gradient clipping (by value) in the optimizer.
discriminator_optimizer = RMSprop(lr = 0.0008,
clipvalue = 1.0,
decay = 1e-8)
discriminator.compile(optimizer = discriminator_optimizer,
loss = 'binary_crossentropy')
In [8]:
# Set discriminator weights to non-trainable
# (will only apply to the `gan` model)
discriminator.trainable = False
gan_input = Input(shape = (latent_dim,))
gan_output = discriminator(generator(gan_input))
gan = Model(gan_input, gan_output)
gan_optimizer = RMSprop(lr = 0.0004,
clipvalue = 1.0,
decay = 1e-8)
gan.compile(optimizer = gan_optimizer,
loss = 'binary_crossentropy')
In [9]:
import os
from keras.preprocessing import image
from keras.datasets import cifar10
In [10]:
# Load CIFAR10 data
(x_train, y_train), (_, _) = cifar10.load_data()
# Select images (class 5)
x_train = x_train[y_train.flatten() == 5]
In [11]:
x_train.shape
Out[11]:
In [12]:
# Normalize data
x_train = x_train.reshape((x_train.shape[0],) + (height, width, channels)).astype('float32') / 255.
iterations = 10000
batch_size = 20
In [13]:
x_train.shape
Out[13]:
In [14]:
save_dir = './data/Chapter 8.5 - Introduction to generative adversarial networks/'
In [15]:
# Start training loop
start = 0
for step in range(iterations):
# Sample random points in the latent space
random_latent_vectors = np.random.normal(size = (batch_size, latent_dim))
# Decode them to fake images
generated_images = generator.predict(random_latent_vectors)
# Combine them with real images
stop = start + batch_size
real_images = x_train[start: stop]
combined_images = np.concatenate([generated_images, real_images])
# Assemble labels discriminating real from fake images
labels = np.concatenate([np.ones((batch_size, 1)),
np.zeros((batch_size, 1))])
# Add random noise to the labels - important trick!
labels = labels + 0.05 * np.random.random(labels.shape)
# Train the discriminator
d_loss = discriminator.train_on_batch(combined_images, labels)
# Sample random points in the latent space
random_latent_vectors = np.random.normal(size = (batch_size, latent_dim))
# Assemble labels that say "all real images"
misleading_targets = np.zeros((batch_size, 1))
# Train the generator (via the gan model,
# where the discriminator weights are frozen)
a_loss = gan.train_on_batch(random_latent_vectors, misleading_targets)
start = start + batch_size
if start > len(x_train) - batch_size:
start = 0
# Occasionally save / plot
if step % 100 == 0:
# Save model weights
gan.save_weights('./saved_checkpoints/Chapter 8.5 - Introduction to generative adversarial networks/gan.h5')
# Print metrics
print('-' * 50)
print('Step: %s' % step )
print('Discriminator loss: %s' % d_loss)
print('Adversarial loss: %s' % a_loss)
# Save one generated image
img = image.array_to_img(generated_images[0] * 255.,
scale = False)
img.save(os.path.join(save_dir, 'generated_dog' + str(step) + '.png'))
# Save one real image, for comparison
img = image.array_to_img(real_images[0] * 255.,
scale = False)
img.save(os.path.join(save_dir, 'real_dog' + str(step) + '.png'))